package com.juanLL.CMC.models;

import android.graphics.Bitmap;
import android.graphics.Rect;

public class Rat extends Model {

	public int hitpoints = 2;
	public Trash stolenTrash;
	public boolean hasTrash = false;
	public boolean isSpawned = false;
	public Rect screenRes;

	public Rat(Rect screenRes, Bitmap bitmap) {
		this.screenRes = screenRes;
		this.bitmap = bitmap;
		this.x = 0 - bitmap.getWidth();
		this.y = screenRes.bottom - bitmap.getHeight();
		this.ySpeed = 0;
		this.xSpeed = 0;
		type = Model.TYPE_RAT;
	}

	@Override
	public void update() {
		super.update();
		if (hasTrash) {
			stolenTrash.x = x;
		}
		if (isOutOfBounds()) {
			reset();
			if (stolenTrash != null) {
				stolenTrash.setState(Trash.STATE_INITIAL);
				stolenTrash = null;
			}
		}
	}

	public void hit() {
		hitpoints--;
		if (hitpoints == 0) {
			die();
		}
	}

	public void grabTrash(Trash trash) {
		stolenTrash = trash;
		stolenTrash.y = y;
		hasTrash = true;
	}

	public void die() {
		dropTrash();
		reset();
	}

	public void dropTrash() {
		hasTrash = false;

		if (stolenTrash != null) {
			stolenTrash.launch(x, y);
			stolenTrash.setState(Trash.STATE_FALLING);
			stolenTrash = null;
		}
	}

	public void goForTrash() {
		xSpeed = 5;
		isSpawned = true;
	}

	public void reset() {
		x = 0 - bitmap.getWidth();
		y = screenRes.bottom - bitmap.getHeight();
		isSpawned = false;
		hasTrash = false;
		hitpoints = 3;
	}

	public boolean isOutOfBounds() {
		return x >= screenRes.right;
	}
}
